package com.lin.wx.pament.component.notify.impl;


import com.alibaba.fastjson2.JSON;
import com.lin.wx.pament.component.model.TransactionNotify;
import com.lin.wx.pament.component.notify.NotifyDataService;
import com.lin.wx.pament.component.notify.NotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author: linjiang
 * @date: 2024/12/10
 */
@Slf4j
@Service
public class NotifyServiceImpl implements NotifyService {
    @Autowired
    private List<NotifyDataService> notifyDataServiceList;

    @Override
    public Map<String, Object> callbackNotify(String product, String body) {
        Optional<NotifyDataService> first = notifyDataServiceList.stream().filter(service -> service.support(product))
                .findFirst();
        if (first.isPresent()) {
            NotifyDataService notifyDataService = first.get();
            TransactionNotify transactionNotify = notifyDataService.breforeNotify(body);
            // 中间可以添加业务逻辑传入下层
            String tradeType = product.replace("-", "_");
            transactionNotify.setTradeType(tradeType);
            transactionNotify.setQueryOrderStatus(1);
            return notifyDataService.afterNotify(transactionNotify);
        }
        log.info("此【{}】产品的回调通知没有对应的处理逻辑。", JSON.toJSONString(product));
        return null;
    }

}
